﻿using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Input;
using FantomNext.Helpers;
using System;

namespace FantomNext.Controls;

public partial class SelectionFrame : UserControl
{
    #region events

    /// <summary>
    /// 当前选矿尺寸发生变化后
    /// </summary>
    public event EventHandler<Rect> SelectionFrameSizeChanged;

    #endregion

    #region fields
    //private Point[] _points = new Point[4];
    private Rect _rect;
    private Point _opposePoint;
    #endregion

    #region ctors
    public SelectionFrame()
    {
        SizeChanged += SelectionFrame_SizeChanged;
        InitializeComponent();
    }
    #endregion

    #region events impls
    private void SelectionFrame_SizeChanged(object? sender, SizeChangedEventArgs e)
    {
        if (IsInitialized == false) return;

        var pa = new Point(0, 0);
        var pb = new Point(Bounds.Width, 0);
        var pc = new Point(Bounds.Width, Bounds.Height);
        var pd = new Point(0, Bounds.Height);

        _rect = new Rect(Bounds.Size);

        UpdateVisual();

        PART_ThumbA.PointerPressed += PART_ThumbCorner_PointerPressed;
        PART_ThumbB.PointerPressed += PART_ThumbCorner_PointerPressed;
        PART_ThumbC.PointerPressed += PART_ThumbCorner_PointerPressed;
        PART_ThumbD.PointerPressed += PART_ThumbCorner_PointerPressed;
        PART_ThumbA.PointerMoved += PART_ThumbCorner_PointerMoved;
        PART_ThumbB.PointerMoved += PART_ThumbCorner_PointerMoved;
        PART_ThumbC.PointerMoved += PART_ThumbCorner_PointerMoved;
        PART_ThumbD.PointerMoved += PART_ThumbCorner_PointerMoved;

    }

    private void PART_ThumbCorner_PointerPressed(object? sender, PointerPressedEventArgs e)
    {
        var currentPointer = e.GetCurrentPoint(this);
        if (currentPointer.Properties.IsLeftButtonPressed == false) return;

        var thumb = sender as Control;
        // 左上角
        if (thumb == PART_ThumbA)
        {
            _opposePoint = _rect.BottomRight;
        }
        // 右上角
        else if (thumb == PART_ThumbB)
        {
            _opposePoint = _rect.BottomLeft;
        }
        // 右下角
        else if (thumb == PART_ThumbC)
        {
            _opposePoint = _rect.TopLeft;
        }
        // 左下角
        else if (thumb == PART_ThumbD)
        {
            _opposePoint = _rect.TopRight;
        }
        else
        {
            throw new NotImplementedException();
        }

        e.Pointer.Capture(thumb);
    }

    private void PART_ThumbCorner_PointerMoved(object? sender, Avalonia.Input.PointerEventArgs e)
    {
        var currentPointer = e.GetCurrentPoint(this);
        if (currentPointer.Properties.IsLeftButtonPressed == false) return;

        var thumb = sender as Control;
        var relativePoint = currentPointer.Position;

        _rect = RectHelper.CreateRect(relativePoint, _opposePoint);

        UpdateVisual();

        SelectionFrameSizeChanged?.Invoke(this, GetEditedRect());
    }
    #endregion

    #region methods


    /// <summary>
    /// 在选框尺寸发生变化后进行对当前控件的更新。
    /// </summary>
    private void UpdateVisual()
    {
        var pa = _rect.TopLeft;// new Point(_rect.Left, _rect.Top);
        var pb = _rect.TopRight;//new Point(_rect.Right, _rect.Top);
        var pc = _rect.BottomRight;//new Point(_rect.Right, _rect.Bottom);
        var pd = _rect.BottomLeft;//new Point(_rect.Left, _rect.Bottom);
        CanvasHelper.SetPosCenter(PART_ThumbA, pa);
        CanvasHelper.SetPosCenter(PART_ThumbB, pb);
        CanvasHelper.SetPosCenter(PART_ThumbC, pc);
        CanvasHelper.SetPosCenter(PART_ThumbD, pd);

        CanvasHelper.SetPosCenter(PART_ThumbTop, (pb - pa) / 2 + pa);
        CanvasHelper.SetPosCenter(PART_ThumbRight, (pc - pb) / 2 + pb);
        CanvasHelper.SetPosCenter(PART_ThumbBottom, (pd - pc) / 2 + pc);
        CanvasHelper.SetPosCenter(PART_ThumbLeft, (pa - pd) / 2 + pd);

        AvaloniaShapeHelper.SetLine(PART_LineAB, pa, pb);
        AvaloniaShapeHelper.SetLine(PART_LineBC, pb, pc);
        AvaloniaShapeHelper.SetLine(PART_LineCD, pc, pd);
        AvaloniaShapeHelper.SetLine(PART_LineAD, pa, pd);

        PART_ThumbLeft.IsVisible = PART_ThumbRight.IsVisible = _rect.Height >= 30;
        PART_ThumbTop.IsVisible = PART_ThumbBottom.IsVisible = _rect.Width >= 30;
    }


    /// <summary>
    /// 获得编辑后的框体。
    /// </summary>
    /// <returns></returns>
    public Rect GetEditedRect()
    {
        return _rect;
    }
    #endregion

}